﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Diagnostics;

namespace RSSReader
{
  public class ParameterizedUri
  {
    public static string BadKey = "~!@#$%^&*()THIS IS A BAD KEY+_)(*&^%$#@!~";
    protected Dictionary<string, string> mParameters;
    protected string mUri;
    protected bool mUriIsRelative;

    public ParameterizedUri(string uri, bool uriIsRelative = true)
    {
      mUri = uri;
      mUriIsRelative = uriIsRelative;
      mParameters = new Dictionary<string, string>();
    }

    public ParameterizedUri(Uri uri)
    {
      fromURI(uri);
    }

    public void addParameter(string name, string value)
    {
      if (!mParameters.ContainsKey(name))
      {
        mParameters.Add(name, value);
      }
    }

    public void updateParameter(string name, string value)
    {
      if (!mParameters.ContainsKey(name))
      {
        addParameter(name, value);
      }
      else
      {
        mParameters[name] = value;
      }
    }

    public void removeParameter(string name, string value)
    {
      if (mParameters.ContainsKey(name))
      {
        mParameters.Remove(name);
      }
    }

    public bool hasParameter(string name)
    {
      return mParameters.ContainsKey(name);
    }

    public string get(string name)
    {
      if (mParameters.ContainsKey(name))
        return mParameters[name];
      else
        return BadKey;
    }

    public string this[string name]
    {
      get
      {
        return get(name);
      }

      set
      {
        updateParameter(name, value);
      }
    }

    public void fromURI(Uri uri)
    {
      mUriIsRelative = !uri.IsAbsoluteUri;
      mParameters = new Dictionary<string, string>();
      string[] parts = uri.ToString().Split('?');
      if (parts.Count() == 2)
      {
        mUri = parts[0];
        string[] parameters = HttpUtility.HtmlDecode(parts[1]).Split('&');
        foreach (string p in parameters)
        {
          string[] nameValueSplit = p.Split('=');
          if (nameValueSplit.Count() == 2)
          {
            string name = nameValueSplit[0],
                   value = nameValueSplit[1];
            addParameter(name, value);
          }
        }
      }
      else if (parts.Count() == 1)
      {
        mUri = parts[0];
      }
    }

    public Uri toURI()
    {
      string fullUri = mUri + "?";
      bool first = true;
      foreach (KeyValuePair<string, string> item in mParameters)
      {
        fullUri += (first?"":"&") + item.Key + "=" + item.Value;
        first = false;
      }
      return new Uri(fullUri, mUriIsRelative?UriKind.Relative:UriKind.Absolute);
    }
  }
}
